package cn.itcast.service.impl;

import cn.itcast.pojo.Weather;
import cn.itcast.service.WeatherService;
import cn.itcast.utils.CityCodeUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.bouncycastle.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class WeatherServiceImpl implements WeatherService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * @param cityName 城市名称
     * @return 5天的城市天气信息
     */
    @Override
    public List<Weather> getCityWeatherInfo(String cityName) {
        //1.根据城市名称获取城市编号
        String cityCode = CityCodeUtils.getCityCode(cityName);
        //2.通过restTemplate发送http请求获取城市添加
        String url = "http://t.weather.itboy.net/api/weather/city/" + cityCode;
        String respBody = restTemplate.getForObject(url, String.class);
        //3.使用FastJSON对结果进行解析
        //3.1将响应的json字符串转化为jsonObject
        JSONObject jsonObject = JSON.parseObject(respBody);
        int status = jsonObject.getIntValue("status");
        if (status == 200) {
            //3.2响应成功,获取data数据
            JSONObject data = jsonObject.getJSONObject("data");
            //3.3 获取forecast
            JSONArray jsonArray = data.getJSONArray("forecast");
            List<Weather> list = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                JSONObject info = jsonArray.getJSONObject(i);
                String date = info.getString("date");
                String high = info.getString("high");
                String low = info.getString("low");
                String ymd = info.getString("ymd");
                String week = info.getString("week");
                String fx = info.getString("fx");
                String fl = info.getString("fl");
                String type = info.getString("type");
                String notice = info.getString("notice");
                Weather weather = new Weather(type, high, low, ymd, week, fx, fl, notice);
                list.add(weather);
            }
            return list;
        }
        return Collections.emptyList();
    }


    public static void main(String[] args) {
        //如下map集合数据，统计出每个国家分别在哪些年举行过世界杯
        Map<String, String> map = new HashMap<String, String>();
        map.put("1930", "乌拉圭");
        map.put("1934", "意大利");
        map.put("1938", "意大利");
        map.put("1950", "乌拉圭");
        map.put("1954", "西德");
        map.put("1958", "巴西");
        map.put("1962", "巴西");
        map.put("1966", "英格兰");
        map.put("1970", "巴西");
        map.put("1974", "西德");
        map.put("1978", "阿根廷");
        map.put("1982", "意大利");
        map.put("1986", "阿根廷");
        map.put("1990", "西德");
        map.put("1994", "巴西");
        map.put("1998", "法国");
        map.put("2002", "巴西");
        map.put("2006", "意大利");
        map.put("2010", "西班牙");
        map.put("2014", "德国");
        //最终数据封装到map集合中，key为国家的名称，value为这个国家举办过世界杯的年份的集合，例如
        // 意大利:[1934,1938,1982,2006]

        // 所有的年集合
        Set<String> strings = map.keySet();

        HashMap<String, List<String>> hm = new HashMap<>();

        // year是所有的年
        for (String year : strings) {

            //通过年获取国家
            String country = map.get(year);

            List s = hm.get(country);

            if (s == null) {
                ArrayList<String> list = new ArrayList<>();
                list.add(year);
                hm.put(country, list);
            } else {
                List<String> list = hm.get(country);
                list.add(year);
                hm.put(country, list);
            }
        }

        System.out.println(hm);
    }

}
